package com.fox.xn.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fox.xn.model.Answer;
import com.fox.xn.model.AnswerItem;

@Service
@Transactional
public class PollService {
	@Autowired
	JdbcTemplate jdbc;
	
	public int doit(Answer an,List<AnswerItem> items){
		jdbc.update("insert into answer(ua,ip,do_at,phone,name) values(?,?,?,?,?)",an.getUa(),an.getIp(),an.getDoAt(),an.getPhone(),an.getName());
		int id = jdbc.queryForObject("select last_insert_id()", Integer.class);
		an.setId(id);
		items.stream().forEach(it->{
			jdbc.update("insert into answer_item(answer_id,answer,question)values(?,?,?)", id,it.getAnswer(),it.getQuestion());
		});
		return id;
	}
}
